package com.luanye.enjoy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luanye.enjoy.entity.Policy;
import com.luanye.enjoy.mapper.PolicyMapper;
import com.luanye.enjoy.service.PolicyService;
import com.luanye.enjoy.utils.BeanCopyUtils;
import com.luanye.enjoy.vo.PolicyDetail;
import com.luanye.enjoy.vo.PolicyVo;
import com.luanye.enjoy.vo.RespBean;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service("policyService")
@AllArgsConstructor
public class PolicyServiceImpl extends ServiceImpl<PolicyMapper, Policy> implements PolicyService {

    private final PolicyMapper policyMapper;

    @Override
    public RespBean getRecommend(Integer pageNum, Integer limit) {
        LambdaQueryWrapper<Policy> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.orderByDesc(Policy::getCreateTime);

        Page<Policy> page = new Page<>(pageNum,limit);
        page(page,queryWrapper);

        List<Policy> records = page.getRecords();

        List<PolicyVo> policyVos = BeanCopyUtils.copyBeanList(records, PolicyVo.class);
        return RespBean.success(policyVos);
    }

    @Override
    public RespBean getPolicyByType(Integer pageNum, Integer limit, Integer typeId) {
        LambdaQueryWrapper<Policy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Policy::getTypeid,typeId);
        Page<Policy> page = new Page<>(pageNum,limit);
        page(page,queryWrapper);

        List<Policy> records = page.getRecords();

        List<PolicyVo> policyVos = BeanCopyUtils.copyBeanList(records, PolicyVo.class);
        return RespBean.success(policyVos);
    }

    @Override
    public RespBean getDetails(Long articleId) {
        Policy policy = getById(articleId);
        PolicyDetail detail = BeanCopyUtils.copyBean(policy, PolicyDetail.class);
        return RespBean.success(detail);
    }

    @Override
    public RespBean selectByKeyword(String keyword) {
        QueryWrapper<Policy> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",keyword);
        List<Policy> policies = policyMapper.selectList(queryWrapper);
        return RespBean.success(policies);
    }
}
